<?php
RegisterPlugin("tpure", "ActivePlugin_tpure");
function ActivePlugin_tpure()
{
    global $zbp;
    $zbp->LoadLanguage('theme', 'tpure');
    Add_Filter_Plugin('Filter_Plugin_Search_Begin','tpure_SearchMain');
}

function tpure_SearchMain() {
    global $zbp;
    foreach ($GLOBALS['Filter_Plugin_ViewSearch_Begin'] as $fpname => &$fpsignal) {
        $fpreturn = $fpname();
        if ($fpsignal == PLUGIN_EXITSIGNAL_RETURN) {
            $fpsignal=PLUGIN_EXITSIGNAL_NONE;return $fpreturn;
        }
    }
    if(!$zbp->CheckRights($GLOBALS['action'])){Redirect('./');}
    $q = trim(htmlspecialchars(GetVars('q','GET')));
    $qc = '<span class=\'schwords\'>' . $q . '</span>';
    $articles = array();
    $category = new Metas;
    $author = new Metas;
    $tag = new Metas;
    $type = 'tpure-search';
    $zbp->title = '搜索 [ ' . $q . ' ] ';
    $template = $zbp->option['ZC_INDEX_DEFAULT_TEMPLATE'];
    if($zbp->template->hasTemplate('search')){
        $template = 'search';
    }
    $w=array();
    if($q){
        $w[]=array('search','log_Content','log_Intro','log_Title',$q);
    }else{
        Redirect('./');
    }
    if(!($zbp->CheckRights('ArticleAll')&&$zbp->CheckRights('PageAll'))){
        $w[]=array('=','log_Status',0);
    }

    $articles = $zbp->GetArticleList(
        '*',
        $w,
        array('log_PostTime' => 'DESC'),
        array($zbp->searchcount),
        null
    );

    foreach($articles as $article){
        $intro = preg_replace('/[\r\n\s]+/', '', trim(SubStrUTF8(TransferHTML($article->Content,'[nohtml]'),100)).'...');
        $article->Intro = str_ireplace($q,$qc,$intro);
        $article->Title = str_ireplace($q,$qc,$article->Title);
    }

    $zbp->header .= '<meta name="robots" content="noindex,follow" />' . "\r\n";
    $zbp->template->SetTags('title', $zbp->title);
    $zbp->template->SetTags('articles',$articles);
    $zbp->template->SetTags('type',$type);
    $zbp->template->SetTags('page',1);
    $zbp->template->SetTags('pagebar',null);
    if($zbp->template->hasTemplate('search')){
        $zbp->template->SetTemplate($template);
    } else {
        $zbp->template->SetTemplate('catalog');
    }
    foreach ($GLOBALS['Filter_Plugin_ViewPost_Template'] as $fpname => &$fpsignal) {
        $fpreturn=$fpname($zbp->template);
    }
    $zbp->template->Display();
    RunTime();
    die();
}